//
//  HOT Toilet ViewController.m
//  Genpara
//
//  Created by AndyChang.zhang on 14-9-23.
//  Copyright (c) 2014年 zhangchh. All rights reserved.
//

#import "HOTToiletViewController.h"
#import "ZZZHttpRequestClient.h"
#import "NSString+extend.h"

#define kFoursquare_host_url @"https://api.foursquare.com/v2/venues/search"

@interface HOTToiletViewController () <NSURLConnectionDelegate, NSURLConnectionDataDelegate>{
    
    GMSMapView *_mapView;
    BOOL firstLocationUpdate_;
    NSMutableData *_mutaData;
    NSDictionary *_venue;
    CLLocationCoordinate2D _userLoc;
}

@end

@implementation HOTToiletViewController

- (id)initWithNibName:(NSString *)nibNameOrNil
               bundle:(NSBundle *)nibBundleOrNil
              userLoc:(CLLocationCoordinate2D)userLocation
                venue:(NSDictionary *)venue
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        _venue = venue;
        _userLoc = userLocation;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self setNaviTitle:@"MAP"];
    NSDictionary *locationDic = [_venue objectForKey:@"location"];
    GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:[[locationDic objectForKey:@"lat"] floatValue]
                                                            longitude:[[locationDic objectForKey:@"lng"]floatValue]
                                                                 zoom:6];

    _mapView  = [GMSMapView mapWithFrame:CGRectZero camera:camera];
    self.view = _mapView;
//    self.hudManager.baseView = self.view;
//    [_mapView addObserver:self
//               forKeyPath:@"myLocation"
//                  options:NSKeyValueObservingOptionNew
//                  context:NULL];
//    [self showLoadingWithMessage:@"定位中..."];
//    // Ask for My Location data after the map has already been added to the UI.
    dispatch_async(dispatch_get_main_queue(), ^{
        _mapView.myLocationEnabled = YES;
        
    });
    //[_mapView addObserver:self forKeyPath:<#(NSString *)#> options:NSKeyValueChangeNewKey context:nil];
    GMSMarker *marker = [[GMSMarker alloc] init];
    marker.title = [[_venue objectForKey:@"name"] toStr];
    
    marker.position = CLLocationCoordinate2DMake([[locationDic objectForKey:@"lat"] floatValue],[[locationDic objectForKey:@"lng"] floatValue]);
    marker.appearAnimation = kGMSMarkerAnimationPop;
    marker.flat = YES;
    marker.draggable = NO;
    marker.groundAnchor = CGPointMake(0.5, 1);
    //marker.icon = [UIImage imageNamed:@"australia"];
    marker.map = _mapView;

    GMSMarker *selfMarker = [[GMSMarker alloc] init];
//    selfMarker.title = [[_venue objectForKey:@"name"] toStr];
    selfMarker.position = _userLoc;
//    selfMarker.map = _mapView;
//    selfMarker.appearAnimation = kGMSMarkerAnimationPop;
//    selfMarker.flat = YES;
//    selfMarker.draggable = NO;
//    selfMarker.groundAnchor = CGPointMake(0.5, 1);

    
    //[self toFitBoundsWithList:@[selfMarker, marker]];
}

- (void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                        change:(NSDictionary *)change
                       context:(void *)context {
    
    
    if (!firstLocationUpdate_) {
        // If the first location update has not yet been recieved, then jump to that
        // location.
        
        firstLocationUpdate_ = YES;
        CLLocation *location = [change objectForKey:NSKeyValueChangeNewKey];
        _mapView.camera = [GMSCameraPosition cameraWithTarget:location.coordinate
                                                         zoom:14];
        
        NSLog(@"定位成功 myLocation: %f, %f",location.coordinate.longitude, location.coordinate.latitude);
        
        
        //[self requestSearchToiletByLoc:location.coordinate];
    }
}

/**
 *  调整mapView的比例 适应于显示全部的大头针
 *
 *  @param markers 大头针数组
 */
- (void)toFitBoundsWithList:(NSArray *)markers {
    GMSCoordinateBounds *bounds;
    for (GMSMarker *marker in markers) {
        if (bounds == nil) {
            bounds = [[GMSCoordinateBounds alloc] initWithCoordinate:marker.position
                                                          coordinate:marker.position];
        }
        bounds = [bounds includingCoordinate:marker.position];
    }
    GMSCameraUpdate *update = [GMSCameraUpdate fitBounds:bounds
                                             withPadding:50];
    [_mapView moveCamera:update];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    //[_mapView removeObserver:self
    //              forKeyPath:@"myLocation"
    //                 context:NULL];
}
@end
